package libra.openfeign.callLocal;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 被OpenFeign调用的Controller
 * @author libra
 */
@RestController
@RequestMapping("local")
public class OpenFeignLocalController {
	private static final Map info = Map.of("from", "local");
	
	// 测试请求头参数接收
	@RequestMapping("header")
	public Object header(@RequestHeader(value="testHeader",required = false) String header, HttpServletRequest req ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "header()", "header", getReqHeads(req), "testHeader", header==null?"null":header) );
		return copyOf;
	}
	
	// 测试请求cookie参数接收
	@RequestMapping("cookie")
	public Object cookie(@CookieValue(value="testCookie",required = false) String cookie, HttpServletRequest req ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "cookie()", "cookie", getCookieInfo(req), "testCookie", cookie==null?"null":cookie) );
		return copyOf;
	}
	
	// 测试请求参数接收
	@RequestMapping("param")
	public Object param( String param ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "param()", "param", param==null?"null":param) );
		return copyOf;
	}
	
	// 测试请求参数接收
	@RequestMapping("restParam/{val}")
	public Object restParam( @PathVariable("val")String val ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "restParam()", "param", val==null?"null":val) );
		return copyOf;
	}
	
	// 测试请求参数接收
	@RequestMapping("json")
	public Object json( @RequestBody Map map ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "json()", "json", map) );
		return copyOf;
	}
	
	// 测试file参数接收
	@RequestMapping("file")
	public Object file( MultipartFile file, String param  ) {
		Map copyOf = new HashMap(info);
		copyOf.putAll( Map.of("method", "file()", "file", file.getOriginalFilename(), "param", param) );
		return copyOf;
	}
	
	
	private Map<String, Object> getReqHeads( HttpServletRequest req ) {
		Enumeration<String> heads = req.getHeaderNames();
		Map<String, Object> headsInfo = new HashMap<>();
		while( heads.hasMoreElements() ) {
			String head = heads.nextElement();
			headsInfo.put(head, req.getHeader(head));
		}
		return headsInfo;
	}
    private Map<String, Object> getCookieInfo(HttpServletRequest request) {
    	Map<String, Object> cookiesMap = new HashMap<String, Object>();
		Cookie[] cookies = request.getCookies();
		if( cookies != null ) {
			for( Cookie cookie : cookies ) {
				cookiesMap.put(cookie.getName(), cookie.getValue());
			}
		} 
		return cookiesMap;
    }
}
